package com.invengo.module_robot.ui.activity;

import android.app.Application;

import androidx.annotation.NonNull;

import com.invengo.library_orionstar.ActionEvent;
import com.invengo.module_robot.data.repsitory.RobotRepsitory;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;

public class RobotViewModel extends BaseViewModel<RobotRepsitory> {
    public RobotViewModel(@NonNull Application application) {
        super(application, RobotRepsitory.getInstance());
        registerVoice();
        registerMessager();
    }

    public UIChangeObservable ui = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<String> launchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<String> backEvent = new SingleLiveEvent<>();
    }

    /**
     * 注册语音
     */
    private void registerVoice() {
        Messenger.getDefault().register(this, "LAUNCH_PAGE", String.class, s -> {
            ui.launchEvent.postValue(s);
        });
        Messenger.getDefault().register(this, "PATH_PARAMS", String.class, s -> {
            KLog.e("跳转的path" + s);
            ui.launchEvent.postValue(s);
        });
    }

    /**
     * 寄存器消息器
     */
    private void registerMessager() {
        Messenger.getDefault().register(this, "OPEARTION_TO_PAGE", ActionEvent.class, action -> {
            Observable.just(action)
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe(event -> {
                        switch (event.getEvent()) {
                            case ActionEvent.ACTION_HOME:
//                                RobotManager.getInstance().launchHome();
                                getUC().getFinishActivityEvent().call();
                                break;
                            case ActionEvent.ACTION_BACK:
                                ui.backEvent.call();
                                break;
                        }
                    });
        });
    }
}
